package Game.Objects;

import java.lang.reflect.InvocationTargetException;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

import GUI.GuiManager;
import GUI.GuiMessenger;
import Game.Level;
import Game.LevelMap;
import Game.Items.Others.GITorch;
import Game.MapBlock.EMapBlockType;
import Game.Objects.Creatures.GOCreature;
import GameMath.GSize;
import Screens.MyScreens.GameScreen;
import Tools.Metrics;
import Tools.Drawing.LayersHelper;
import Tools.Lighting.ILight;
import Tools.Lighting.LightPoint;
import Tools.Lighting.LightPoint.EAnimations;

/** Game object - Torch
 * @author Aikidos
 */
public class GOTorch extends GameObject {

	/** Light */
	private LightPoint _light;
	
	/** Light range */
	private int _lightRange = 15;
	
	public GOTorch(ObjectManager objectManager) throws SlickException {
		super(objectManager, "Torch");
		
		setSprite(new Image("res/gfx/objects/torch.png"));
		setLayerForDrawing(LayersHelper.LAYER_OBJECTS);
		_isCanBeUsed = true;
	}
	
	/** Initialize light */
	public void initLight() {
		_light = new LightPoint(_objectManager.getLevel().getLightManager());
		_light.setRange(_lightRange);
		_light.setPosition(getCoord());
		_objectManager.getLevel().getLightManager().addLight(_light);
		
		// Open around map blocks
		LevelMap levelMap = _objectManager.getLevel().getMap();
		GSize mapSize = _objectManager.getLevel().getMap().getSize();
		for (int i = 0; i < mapSize.width; i++) {
			for (int j = 0; j < mapSize.height; j++) {
				if ((levelMap.getBlocks()[i][j].Type != EMapBlockType.None)
						&& !levelMap.getBlocks()[i][j].IsOpen) {
					Vector2f blockPos = new Vector2f(
							(i * Level.BLOCK_SIZE) + (Level.BLOCK_SIZE / 2), 
							(j * Level.BLOCK_SIZE) + (Level.BLOCK_SIZE / 2));

					float dist = Metrics.getDistance(this.Position, blockPos);
					if (dist <= (_lightRange * Level.BLOCK_SIZE)) {
						levelMap.getBlocks()[i][j].IsOpen = true;
					}	
				}
			}
		}
		
		// Recalculate light map
		_objectManager.getLevel().getLightManager().calcLighting();
	}

	@Override
	public void use(GameObject user) throws SlickException,
			InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException,
			NoSuchMethodException, SecurityException, ClassNotFoundException {
		
		_objectManager.destroyObject(this);
		
		if (user.isPlayer()) {
			GOPlayer player = (GOPlayer) user;
			player.getInventory().addItem(new GITorch());
			
			// Show message
			GuiManager.getMessenger().addMessage("[green]You[/green] picked up a [yellow]" + getName() + "[/yellow]");
		}
		
		if (user.isCreature()) {
			GOCreature creature = (GOCreature) user;
			creature.getInventory().addItem(new GITorch());
		}
	}

	@Override
	public void destroy() {
		// Remove light
		_objectManager.getLevel().getLightManager().removeLight(_light);
		_objectManager.getLevel().getLightManager().calcLighting();
	}
	
}
